<?php $this->headLink()
->appendStylesheet($this->Css('button.css'))
->appendStylesheet($this->js('css/jquery.alerts.css'));
$this->headScript()
->appendFile($this->js('plugins/jquery.cookies.min.js'))
->appendFile($this->js('plugins/jquery.form.js'))
->appendFile($this->js('plugins/jquery.blockUI.js'))
->appendFile($this->js('plugins/jquery.alerts.js'))
->appendFile($this->js('plugins/jquery.stickyfloat.js'));

if ($this->yearSemester)
 $yearSemester = $this->yearSemester;
 else
 $yearSemester = $this->Semester['yearSeme'];

 if ($this->setYearSemester)
  $setYearSemester = $this->setYearSemester;
 else
 $setYearSemester = $this->Semester['yearSeme'];

 if ($this->classSn)
 $classSn = $this->classSn;
 else
 $classSn = $this->lowYear.'-1';
 ?>
 <style>
 #content-body {width:100%; margin:0 auto; position:relative; }
 #selectMenu {position:absolute; left:10px; width:150px; height:450px;text-align:center; background: #f1f1f1;padding:2px;border:#ccc 1px dotted;  }
   #selectMenu select ,  #selectMenu input {width:100%}
  #selectMenu p {margin: 5px}
  #list-data {margin-left:180px;}
 .option_boy {color:blue;}
 .option_girl {color: red}
  #list-data fieldset {margin-left: 3px  }
  #list-data fieldset ul {margin:0px}
  #list-data fieldset li {float:left; margin:1px 10px; list-style-type: none;}

  .floatDiv label{ float:left}
  .floatDiv dd{ float:left; margin-left:5px; margin-right:30px}
  .checked {background: #ff0; }
 </style>
 <script>
 $(function(){
	 var guidanceSelect = <?php echo Zend_Json::encode($this->form->guidanceSelect)?>;
	 var guidanceOptions =   <?php echo Zend_Json::encode(array_keys($this->form->optionArray))?>;
	 $(document).ajaxStop($.unblockUI);
	  setStudentOption();


	  $("#list-data fieldset").each(function(){
		  $(this).addClass('ui-widget-content ui-corner-all');
		  });
	   $("#list-data legend").each(function(){
	          $(this).addClass('ui-widget-header');
	          });



	  function setStudentOption() {
	    	 var sn = $("#studentSn").val();
	         if (!sn ) {
	    	  sn = $.cookies.get("studentSn");
	    	 if (!sn)
		    	 sn = $("#studentSn option:first").attr('value');
	         }
	         var setYearSemester = $("#setYearSemester").val();
	         if (!setYearSemester) {
	        	 setYearSemester = $.cookies.get("setYearSemester");
	        	 if (!setYearSemester)
	        		 yearSemester = $("#setYearSemester option:first").attr('value');
	         }

	          $("#studentSn").val(sn);
	          $("#set-student_sn").val(sn);
	          $("#setYearSemester").val(setYearSemester);
	          $.cookies.set('studentSn', sn);
	          $.cookies.set('setYearSemester', setYearSemester);

	          $.get('<?php echo $this->route("list-data")?>',
	    	        {studentSn:sn ,setYearSemester: setYearSemester},
	    	          function(data){
	    	        	$("#list-data input[type='checkbox']").attr('checked','').parent().removeClass('checked');
	    	        	$("#list-data table select").val('');
	    	        	$("#list-data  #set-sn").attr('value','');
	    	        	if (data) {
	    	        		$("#list-data #set-sn").attr('value',data.sn);
	    	        	    $.each(guidanceSelect , function(key,val){
	    	        	        $("#"+val).val(data[val]);
		    	        	 });

	    	        	    $.each(guidanceOptions, function(key,val){
	    	        	         if (data[val]) {
	    	        	    	   var tt= data[val].split(',');
	    	        	    	   $.each(tt,function(i,v){
    			    	          if (v) {
    			    	        	 $("#"+val+"_"+v).attr('checked',true).parent().addClass('checked');
    				    	          }
    			    	          });
	    	        	         }
	    	        	    });

	    	        	}
	             });
		 }

	   $("#list-data input[type='checkbox']").click(function(){
		    if ($(this).attr('checked'))
			    $(this).parent().addClass('checked');
		    else
		    	$(this).parent().removeClass('checked');
		 });

	    $("#yearSemester").change(function(){
	    	 $.cookies.del('studentSn');
	        $("#setForm").submit();
		  });
	   $("#classSn").change(function(){
		   $("#setForm").enable(false).submit();
		   $.cookies.del('studentSn');
		   $("#setForm").submit();
       });

	    $("#studentSn, #setYearSemester").change(	setStudentOption);

	    $('#signForm').ajaxForm({ success: showResponse});

	    function showResponse(data) {
		    if (data) {
			    if ($("#auto_next").attr('checked')) {
				    var ss = $("#studentSn option:selected").next();
				    if (ss) {
		                $(ss).attr('selected','selected');
		                $("#studentSn").trigger('change');
		            }
			    }
//			    else
//		    	jAlert('<?php echo $this->translate("save complated")?>');
		    }
	    }

	    // 儲存
	    $("#saveBtn").click(function(){
	    	$.blockUI({ message: '<h1><img src="<?php echo $this->baseUrl()?>/images/loading.gif" /> <?php echo $this->translate("Just a moment..")?>.</h1>' });
	    	$('#signForm').submit();
		 });

	    $("#copyPrevBtn").click(function(){
	    	  $.blockUI({ message: '<h1><img src="<?php echo $this->baseUrl()?>/images/loading.gif" /> <?php echo $this->translate("Just a moment..")?>.</h1>' });
	        var studentSn = $("#studentSn").val();
	        var yearSemester = $("#setYearSemester").val();
	        var setSn = $("#set-sn").attr('value');
	    	$.get('<?php echo $this->route("copy")?>',
	    	    	{studentSn:studentSn, yearSemester:yearSemester, setSn:setSn},
	    	    function(data){
	    	    		 if (data) {
	    	                 if ($("#auto_next").attr('checked')) {
	    	                     var ss = $("#studentSn option:selected").next();
	    	                     if (ss) {
	    	                         $(ss).attr('selected','selected');
	    	                     }
	    	                 }
	    	                 $("#studentSn").trigger('change');
	    	             }
	    	    		 else
	                           jAlert('<?php echo $this->translate("no data")?>');
    	    	}
	    	);
		});

	    $('#selectMenu').stickyfloat({ duration: 450 });

});
 </script>
<div class="ui-widget" id="content-body">
<div id="selectMenu">
<form method="post" action="" id="setForm" >
<p>
<select name="yearSemester" id="yearSemester">
<?php echo $this->YearSemesterOptions($yearSemester,array('order'=>'year DESC'))?>
</select>
</p>
<p>
<select name="classSn" id="classSn">
<?php echo $this->classNameOptions($yearSemester,$classSn)?>
</select>
</p>
<p>
<select name="studentSn" id="studentSn" size="15">
<?php echo $this->StudentNameOptions($yearSemester,$classSn, $this->studentSn)?>
</select>
</p>
<p>
<input type="button"  id="saveBtn" value="<?php echo $this->translate('saveBtn')?>" />
</p>
<p>
<input type="checkbox" name="auto_next"  id="auto_next" value="1" />
<label for="auto_next"><?php echo $this->translate('auto next student')?></label>

</p>
</form>
</div>
<div id="list-data">
<form action="<?php echo $this->route('save')?>" method="post" id="signForm" >
<input type="hidden" name="studentSn"  id="set-student_sn" />
<input type="hidden" name="sn"  id="set-sn" />
<select name="setYearSemester" id="setYearSemester" >
<?php echo  $this->yearSemesterOptions($setYearSemester, array('order'=>'year DESC'))?>
</select>
<input type="button" id="copyPrevBtn" value="<?php echo $this->translate('copy prev semester data')?>" />
<table>
<tr>
<td class="floatDiv">
<?php echo $this->form->parent_relation?>
<?php echo $this->form->family_kind?>
<?php echo $this->form->family_air?>
</td>
</tr>
<tr>
<td  class="floatDiv">
<?php echo $this->form->discipline_father?>
<?php echo $this->form->discipline_mother?>
<?php echo $this->form->live_state?>
<?php echo $this->form->asset_state?>
</td>
</tr>
<tr>
<td>
<?php foreach($this->form->optionArray as $id=>$val):?>
    <?php echo $this->form->{$id}?>
 <?php endforeach?>
</td>
</tr>
</table>
</form>
</div>


<div style="clear:both"></div>
</div>